package robot

import (
	"device-admin/internal/app/grpc/protoc/robotc"
	"device-admin/internal/app/web/service/cmd"

	"192.168.1.75/go-pkg/errorx"
	"google.golang.org/protobuf/proto"
)

// GetOrderVideoInfo 获取订单视频信息
func GetOrderVideoInfo(robotID string, orderID string) (fileName string, fizeSize int64, md5 string, err error) {
	videoReq := &robotc.SalesVideoReq{}
	videoReq.OrderId = []byte(orderID)

	msgType := string(videoReq.ProtoReflect().Descriptor().FullName())
	msgContent, _ := proto.Marshal(videoReq)
	msgID, err := wildmsgCMDPack(robotID, msgType, msgContent)
	if err != nil {
		return "", 0, "", errorx.New("下发指令失败", -1)
	}

	// 等待下发结果
	rspData, err := cmd.WaitCmdRsp(robotID, "wildmsg", msgID, "95")
	if err != nil {
		return "", 0, "", errorx.New("获取订单视频信息失败", -1)
	}

	// 解析相应结果
	videoRsp := &robotc.SalesVideoRsp{}
	proto.Unmarshal(rspData, videoRsp)

	// 返回结果
	if videoRsp.Status == 1 {
		return string(videoRsp.FileName), videoRsp.FileSize, string(videoRsp.Md5), nil
	}
	return "", 0, "", errorx.New("获取订单视频信息失败", -1)
}

// GetOrderVideoContent 获取订单出货视频内容
func GetOrderVideoContent(robotID string, startOffet int64, packageSize int64) (content []byte, err error) {
	salesVideoContentReq := &robotc.SalesVideoContentReq{
		PackageSize: int32(packageSize),
		StartOffset: startOffet,
	}
	msgType := string(salesVideoContentReq.ProtoReflect().Descriptor().FullName())
	msgContent, _ := proto.Marshal(salesVideoContentReq)
	msgID, err := wildmsgCMDPack(robotID, msgType, msgContent)
	if err != nil {
		return []byte{}, errorx.New("命令下发失败", -1)
	}

	// 等待下发结果
	rspData, err := cmd.WaitCmdRsp(robotID, "wildmsg", msgID, "95")
	if err != nil {
		return []byte{}, errorx.New("获取视频出错", -1)
	}

	// 解析相应结果
	videoRsp := &robotc.SalesVideoContentRsp{}
	proto.Unmarshal(rspData, videoRsp)

	if videoRsp.Status == 1 {
		return videoRsp.Content, nil
	} else {
		return []byte{}, errorx.New("获取视频内容失败", -1)
	}
}
